/*******************************************************************************
 * Copyright (c) 2010 liw.
 * All rights reserved. 
 * 
 * This file is part of VanBus.
 * 
 *     VanBus is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     VanBus is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with VanBus.  If not, see <http://www.gnu.org/licenses/>.
 * Contributors:
 *     liw - initial API and implementation
 ******************************************************************************/
package org.niclab.vanbus.model;

import org.niclab.vanbus.activity.controller.BusRouteController;

import android.content.Context;
import android.os.Bundle;


public class Reminder {
	
	public enum ReminderType{
		StartStopReminder("S") , EndStopReminder("E");
		
		private final String type;
		
		private ReminderType(String type){
			this.type = type;
		}
		
		public String getTypeStr(){
			return type;
		}
		
		public static ReminderType parseReminderType(String type){
			
			for(ReminderType t : ReminderType.values()){
				
				if(type.equals(t.getTypeStr()))
					return t;
			}
			
			return null;
		}
		
		
	}
	
	private ReminderType type;
	
	private String reminderId;
	
	private String description;
	
	protected BusStop stop;
	
	protected BusRoute route;
	
	//constructor
	
	public Reminder(ReminderType type, BusRoute route, BusStop stop){
		this.type = type;
		this.route = route;
		this.stop =stop;
		
	}
	//getters and setters
	public ReminderType getType() {
		return type;
	}
	public void setType(ReminderType type) {
		this.type = type;
	}
	
	public String getReminderId() {
		if(reminderId == null || reminderId.equals("")){
			generateReminderId();
		}
		
		return reminderId;
	}
	public void setReminderId(String id) {
		this.reminderId = id;
	}
	
	//Id = type + routeId + routeDir + stopId
	private void generateReminderId(){
		
			StringBuilder strBuilder = new StringBuilder();
			strBuilder.append(type.getTypeStr());
			strBuilder.append(route.getRouteId());
			strBuilder.append(route.getRouteDir());
			strBuilder.append(stop.getStopId());
			
			setReminderId(strBuilder.toString());	
		
	}
	

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public BusStop getStop() {
		return stop;
	}

	public void setStop(BusStop stop) {
		this.stop = stop;
	}

	public BusRoute getRoute() {
		return route;
	}

	public void setRoute(BusRoute route) {
		this.route = route;
	}
	
	public Bundle createBundle(){
		Bundle modelBundle = new Bundle();

		modelBundle.putString("reminderType",type.getTypeStr());
		Bundle routeBundle = new Bundle();
		routeBundle.putString("routeId", route.getRouteId());
		routeBundle.putString("routeDir", route.getRouteDir());
		
		modelBundle.putBundle("busRoute", routeBundle);

		
		return modelBundle;
	
	}
	
	public Reminder(Bundle bundle,Context context){
		
		this.type = ReminderType.parseReminderType(bundle.getString("reminderType"));
		
		BusRouteController routeCtr = new BusRouteController();
		
		Bundle routeBundle = bundle.getBundle("busRoute");
		String routeId = routeBundle.getString("routeId");
		String routeDir = routeBundle.getString("routeDir");
		this.route = routeCtr.createBusRouteFromCache(routeId, routeDir,null,null);
	
		
		
	}
	

}
